//id type
var scene = parseURL().scene;
var id = scene.split("_")[0];
var type = scene.split("_")[1];
var token = parseURL().token;
var sh = parseURL().sh;

if (sh == 812) {
  document.getElementById("redeemNow").classList.add("redeemNowX");
  document.getElementById("redeemLater").classList.add("redeemLaterX");
}

document.addEventListener("UniAppJSBridgeReady", function () {
  uni.getEnv(function (res) {
    console.log("当前环境：" + JSON.stringify(res));
  });
});

var container = document.getElementById("bm");
let animPath = type + ".json";

var animData = {
  container: container,
  renderer: "svg",
  loop: false,
  prerender: false,
  autoplay: true,
  autoloadSegments: false,
  path: animPath
};

var anim;
var isBrethning = false;

anim = bodymovin.loadAnimation(animData);
anim.addEventListener("DOMLoaded", startAnimation);

function hitRedeemNow() {
  if (isBrethning == true) {
    return;
  }

  isBrethning = true;

  anim.playSegments([131, 171], true);

  ajax({
    method: "POST",
    url: "https://miner.valuechain.group/api/cloud/receivedCloudPower/" + scene,
    header: {
      Authorization: "Bearer " + token
    },
    success: function (res) {
      isBrethning = false;
      if (res.code == 200) {
        anim.addEventListener("complete", hitComplete);
        anim.playSegments([171, 184], true);
      } else {
        alert(res.message);
      }
    }
  });
}

function hitRedeemLater() {
  if (uni) {
    uni.postMessage({
      data: {
        action: "delayRedeemAction"
      }
    });
  }

  if (uni) {
    uni.redirectTo({
      url: "/pages/index/index"
    });
  }
}

function startAnimation() {
  anim.playSegments([
    [0, 67],
    [67, 115]
  ], true);
}

function hitComplete() {
  if (uni) {
    uni.postMessage({
      data: {
        action: "nowRedeemAction"
      }
    });
  }

  if (uni) {
    uni.redirectTo({
      url: "/pages/index/index"
    });
  }
}

function parseURL(_url) {
  var queryString = "";

  var url = _url || location.href;

  var index = url.lastIndexOf("?");

  var map = {};
  /* 如果当前 url 没有 query-string*/
  if (index > -1) {
    var queryString = url.slice(index).split("?")[1];
    queryString.split("&").forEach(function (item) {
      var t = item.split("=");
      map[t[0]] = t[1];
    });
  }

  return map;
}

/* 封装ajax函数
 * @param {string}opt.type http连接的方式，包括POST和GET两种方式
 * @param {string}opt.url 发送请求的url
 * @param {boolean}opt.async 是否为异步请求，true为异步的，false为同步的
 * @param {object}opt.data 发送的参数，格式为对象类型
 * @param {function}opt.success ajax发送并接收成功调用的回调函数
 */
function ajax(opt) {
  opt = opt || {};
  opt.method = opt.method.toUpperCase() || "POST";
  opt.url = opt.url || "";
  opt.async = opt.async || true;
  opt.data = opt.data || null;
  opt.header = opt.header || null;
  opt.success = opt.success || function () { };

  var xmlHttp = null;
  if (XMLHttpRequest) {
    xmlHttp = new XMLHttpRequest();
  } else {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }

  var params = [];
  for (var key in opt.data) {
    params.push(key + "=" + opt.data[key]);
  }

  var postData = params.join("&");
  if (opt.method.toUpperCase() === "POST") {
    xmlHttp.open(opt.method, opt.url, opt.async);
    xmlHttp.setRequestHeader("Content-Type", "application/json");
    xmlHttp.setRequestHeader("Authorization", opt.header["Authorization"]);
    xmlHttp.send(postData);
  } else if (opt.method.toUpperCase() === "GET") {
    xmlHttp.open(opt.method, opt.url + "?" + postData, opt.async);
    xmlHttp.send(null);
  }

  xmlHttp.onreadystatechange = function () {
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
      opt.success(JSON.parse(xmlHttp.responseText)); //如果不是json数据可以去掉json转换
    }
  };
}